/******************************************************************
 *
 *    山东中软科
 *
 *    Copyright (c) 2019-2021
 *
 *    Package:     io.finer.erp.jeecg.stock.service.impl
 *
 *    Filename:    Scan4thServiceImpl.java
 *
 *    Description: 
 *
 *    Copyright:   Copyright (c) 2019-2021
 *
 *    Company:     山东中软科
 *
 *    @author:     duany
 *
 *    @version:    1.0.0
 *
 *    Create at:   2021-09-30 10:11:29
 *
 *    Revision:
 *
 *    2021-09-30 10:11:29 - first revision by duany
 *
 *    Function List:
 *
 *****************************************************************/

package io.finer.erp.jeecg.stock.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.api.client.util.Maps;

import cn.hutool.core.collection.CollUtil;
import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.bas.entity.BasMaterialCategory;
import io.finer.erp.jeecg.bas.entity.BasMeasureUnit;
import io.finer.erp.jeecg.bas.service.IBasMaterialCategoryService;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.IBasMeasureUnitService;
import io.finer.erp.jeecg.stock.entity.StkInventory;
import io.finer.erp.jeecg.stock.mapper.StkInventoryMapper;
import io.finer.erp.jeecg.stock.service.IScan4thService;
import io.finer.erp.jeecg.stock.service.IStkInventoryService;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName Scan4thServiceImpl
 * @Description 
 * @author duany
 * @Date 2021-09-30 10:11:29
 * @version 1.0.0
 */
@Service
@Slf4j
public class Scan4thServiceImpl implements IScan4thService {
	@Autowired
	private IBasMaterialService materialService;
	@Autowired
	private IBasMaterialCategoryService categoryService;
	@Autowired
	private IBasMeasureUnitService unitService;
	@Autowired
	private IStkInventoryService inventoryService;
	@Autowired
	private StkInventoryMapper inventoryMapper;

	@Override
	public Result<?> rePrintZjMaterial(String code) {
		BasMaterial material = this.materialService.getMaterialByCode(code);
		if(null == material) {
			return Result.error("未找到物料");
		}
		String categoryId = material.getCategoryId();
		BasMaterialCategory category = this.categoryService.getById(categoryId);
		if(null == category) {
			return Result.error("未找到物料分类");
		}
		String unitId = material.getUnitId();
		BasMeasureUnit unit = unitService.getById(unitId);
		String materialId = material.getId();
		List<StkInventory> inventoryList = inventoryService.lambdaQuery().eq(StkInventory::getMaterialId, materialId).gt(StkInventory::getQty, BigDecimal.ZERO).list();
		if(CollUtil.isEmpty(inventoryList)) {
			return Result.error("未找到该物料的库存记录");
		}
		StkInventory inventory = inventoryList.get(0);
		BigDecimal qty = inventory.getQty();
		BigDecimal cost = inventory.getCost();
		
		Map<String, Object> result = Maps.newHashMap();
		result.put("categoryId", categoryId);
		result.put("unitName", unit.getName());
		result.put("qty", qty);
		result.put("cost", cost);
		result.put("code", code);
		return Result.ok(result);
	}
}
